const express=require('express');
//引入连接池模块
const pool=require("../pool.js");
//创建路由器对象
const r=express.Router();
//添加路由
//1.登录模块
r.get("/v1/login/:uname&:upwd",(req,res)=>{
	var _uname=req.params.uname;
	var _upwd=req.params.upwd;
	var sql="select*from xz_user where uname=? and upwd=?"
	pool.query(sql,[_uname,_upwd],(err,result)=>{
		if(err) throw err;
		if(result.length>0){
			res.send("1")
		}else{
			res.send("0")
		}
	})
})
//2.用户列表
r.get("/v1/list",(req,res)=>{
	var sql="select*from xz_user"
	pool.query(sql,(err,result)=>{
		if(err) throw err;
		res.send(result);
	})
})
//3.根据uid删除用户
r.delete("/v1/del/:uid",(req,res)=>{
	var _uid=req.params.uid;
	var sql="delete from xz_user where uid=?"
	pool.query(sql,[_uid],(err,result)=>{
		if(err) throw err;
		if(result.affectedRows>0){
			res.send("1")
		}else{
			res.send("0")
		}
	})
})
//4.根据uid查询用户
r.get("/v1/search/:uid",(req,res)=>{
	var _uid=req.params.uid;
	var sql="select*from xz_user where uid=?"
	pool.query(sql,[_uid],(err,result)=>{
		if(err) throw err;
		res.send(result)
	})
})
//5.根据uid修改用户信息
r.put("/v1/update",(req,res)=>{
	var obj=req.body;
	var sql="update xz_user set ? where uid=?";
	pool.query(sql,[obj,obj.uid],(err,result)=>{
		if(err) throw err;
		if(result.affectedRows>0){
			res.send("1")
		}else{
			res.send('0')
		}		
	})
})
//6.根据uname查询用户
r.get ("/v1/getUname/:uname",(req,res)=>{
	var _uname=req.params.uname;
	var sql="select*from xz_user where uname=?"
	pool.query(sql,[_uname],(err,result)=>{
		if(err) throw err;
		if(result.length>0){
			 res.send("1")
		}else{
			res.send('0')
		}
	})
})
//7.注册用户
r.post("/v1/reg",(req,res)=>{
	var sql="insert into xz_user values ?"
	pool.query(sql,[req.body],(err,result)=>{
		if(err) throw err;
		
	})
})
//导出路由器对象
module.exports=r;
